//
// Created by wlk12 on 2024/5/5.
//
#include <iostream>
#include <string>
#include <thread>
#include <utility>
#include <sstream>

class A
{
public:
    explicit A(std::string name) : name(std::move(name))
    {
        std::stringstream ss;
        ss << "Construct A:" << this->name << ", Thread: " << std::this_thread::get_id() << std::endl;
        std::cout << ss.str();
    }

    ~A()
    {
        std::stringstream ss;
        ss << "Destruct A:" << name << ", Thread: " << std::this_thread::get_id() << std::endl;
        std::cout << ss.str();
    }

    std::string name;
};

A a("global");

int globalIntFun()
{
    std::cout << "globalIntFun: " << std::this_thread::get_id() << std::endl;
    return 1;
}

int n = globalIntFun();

int main()
{
    std::cout << "Main Thread: " << std::this_thread::get_id() << std::endl;

    static A ma("main static");

    auto lambda = [](std::string threadName){
        std::stringstream ss;
        ss << "Sub Thread: " << threadName << ", ID: " << std::this_thread::get_id() << std::endl;
        std::cout << ss.str() ;

        static A ta(threadName + " static");

        thread_local A tls(threadName + " TLS");

        auto f = [](){
            std::cout << ta.name << "  " << tls.name << std::endl;
        };
        f();
    };

    std::thread t1(lambda, "T1");
    std::thread t2(lambda, "T2");
    t2.join();
    t1.join();


    return 0;
}